{% template "admin/header.html" %}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>权限列表</legend>
    </fieldset>
    <div class="">
        <a href="/admin/menu/addUI" class="layui-btn layui-btn-sm">添加节点</a>
        <a class="layui-btn layui-btn-normal layui-btn-sm"  onclick="openAll();">展开或折叠全部</a>
    </div>
    <table class="layui-table" id="treeTable" lay-filter="treeTable"></table>
</div>

<script type="text/html" id="auth">
    <input type="checkbox" name="authopen" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="authopen" {{is_auth(d.authopen)}}>
</script>
<script type="text/html" id="status">
    <input type="checkbox" name="menustatus" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="menustatus" {{is_auth(d.menustatus)}}>
</script>
<script type="text/html" id="order">
    <input name="{{d.id}}" data-id="{{d.id}}" class="list_order layui-input" value=" {{d.sort}}" size="10"/>
</script>
<script type="text/html" id="icon">
    <span class="icon {{d.icon}}"></span>
</script>

<script type="text/html" id="action">
    <a href="/admin/menu/addUI?pid={{d.id}}" class="layui-btn layui-btn-normal layui-btn-xs">添加</a>
    <a href="/admin/menu/editUI?id={{d.id}}" class="layui-btn layui-btn-xs">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="topBtn">
    <a href="/admin/menu/addUI" class="layui-btn layui-btn-sm">添加权限</a>
</script>

{% template "admin/footer.html" %}

<script>
    function is_auth(is) {
        return is==1?'checked':'';
    }

    var editObj=null,ptable=null,treeGrid=null,tableId='treeTable',layer=null;
    layui.config({
        base: '/static/plugins/layui/extend/'
    }).extend({
        treeGrid:'treeGrid'
    }).use(['jquery','treeGrid','layer','form'], function(){
        var $=layui.jquery;
        treeGrid = layui.treeGrid;
        layer=layui.layer;
        form = layui.form;
        var loading =layer.load(0, {shade: [0.1,'#fff']});
        ptable=treeGrid.render({
            id:tableId
            ,elem: '#'+tableId
            ,idField:'id'
            ,url:'/admin/menu/query '
            ,cellMinWidth: 100
            ,treeId:'id'//树形id字段名称
            ,treeUpId:'pid'//树形父id字段名称
            ,treeShowName:'title'//以树形式显示的字段
            ,height:'full-140'
            ,isFilter:false
            ,where:{}
            ,iconOpen:true//是否显示图标【默认显示】
            ,isOpenDefault:false//节点默认是展开还是折叠【默认展开】
            ,cols: [[
                {field: 'id', title: 'ID', width: 70},
                {field: 'icon', align: 'center',title: '图标', width: 60,templet: '#icon'},
                {field: 'title', title: '权限名称', width: 200},
                {field: 'href', title: '路由', width: 200},
                {field: 'authopen',align: 'center', title: '是否验证权限', width: 150,toolbar: '#auth'},
                {field: 'menustatus',align: 'center',title: '菜单状态', width: 150,toolbar: '#status'},
                {field: 'sort',align: 'center', title: '排序', width: 80, templet: '#order'},
                {width: 160,align: 'center', toolbar: '#action'}
            ]]
            ,page:false
            ,done:function (res) {
                layer.close(loading);
            }
        });

        treeGrid.on('tool('+tableId+')',function (obj) {
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('您确定要删除该记录吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("/admin/menu/delete",{id:data.id},function(res){
                        layer.close(loading);
                        if(res.Code==1){
                            layer.msg(res.Msg,{time:1000,icon:1});
                            obj.del();
                        }else{
                            layer.msg(res.Msg,{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });

        //是否验证
        form.on('switch(authopen)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var authopen = obj.elem.checked===true?1:0;
            $.post('/admin/menu/edit',{'id':id,'authopen':authopen},function (res) {
                layer.close(loading);
                if (res.Code==1) {
                    treeGrid.render;
                }else{
                    layer.msg(res.Msg,{time:1000,icon:2});
                    treeGrid.render;
                    return false;
                }
            })
        });

        //状态
        form.on('switch(menustatus)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var menustatus = obj.elem.checked===true?1:0;
            $.post('/admin/menu/edit',{'id':id,'menustatus':menustatus},function (res) {
                layer.close(loading);
                if (res.Code==1) {
                    treeGrid.render;
                }else{
                    layer.msg(res.Msg,{time:1000,icon:2});
                    treeGrid.render;
                    return false;
                }
            })
        });

        $('body').on('blur','.list_order',function() {
            var id = $(this).attr('data-id');
            var sort = $(this).val();
            $.post('/admin/menu/edit',{id:id,sort:sort},function(res){
                if(res.Code==1){
                    layer.msg(res.Msg,{time:1000,icon:1});
                }else{
                    layer.msg(res.Msg,{time:1000,icon:2});
                    treeGrid.render;
                }
            })
        });
    });

    function openAll() {
        var treedata=treeGrid.getDataTreeList(tableId);
        treeGrid.treeOpenAll(tableId,!treedata[0][treeGrid.config.cols.isOpen]);
    }
</script>
</body>
</html>
